package com.itheima.Test;

import com.itheima.domain.Student;
import com.itheima.util.NameUtil;
import com.itheima.util.RandomUtil;

import java.time.LocalDate;
import java.time.Period;
import java.util.*;
import java.util.stream.Collectors;

/*
生成70个 学生信息

男生和女生各自有多少人？    可以
00后有多少人？    可以
名字长度是2的学生有多少人？ 可以
名字长度是3的学生有多少人？ 可以
姓张的同学有多少人？  可以

手机号中至少包含2个6的有哪些？  可以

手机号中至少包含1个6和1个8的有哪些？  可以
有2个的微信号的人个数  可以
有1个的微信号的人个数   可以

各个小组的平均年龄？
最大三个年纪的
最小三个年纪的
最大年纪的人和最小年纪的人是谁，差多久?
不同的姓各自有多少学生？
不同的年龄段有多少人？
 */
public class Test01 {

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

        for (int i = 1; i <= 70; i++) {
            Student student = new Student();
            if (i <= 9) {
                student.setSid("A000" + i);
            } else {
                student.setSid("A00" + i);
            }
            student.setName(RandomUtil.getXing() + RandomUtil.getMing());
            student.setGender(RandomUtil.getGender());
            student.setBirthday(RandomUtil.getBirthday());
            student.setGroup((i - 1) / 5 + 1);
            student.setPhone(RandomUtil.getPhone());
            student.setWxs(RandomUtil.getWxs());
            list.add(student);

        }
        for (Student student : list) {
            System.out.println(student);
        }
        System.out.println("男生的数量是" + getNan(list));
        System.out.println("女生的数量是" + getNv(list));
        System.out.println("00后的数量是" + get00(list));
        System.out.println("名字长度为2的数量是" + getName2(list));
        System.out.println("名字长度为3的数量是" + getName3(list));
        System.out.println("姓张的数量是" + getZhang(list));
        System.out.println("手机号中至少有两个6数量是" + getPhone66(list));
        System.out.println("手机号中至少有一个6 一个8数量是" + getPhone68(list));
        System.out.println("有两个微信号的数量是" + get2WX(list));
        System.out.println("有一个个微信号的数量是" + get1WX(list));
        //System.out.println("名字长度为3的数量是"+getName3(list));
        Map<Integer, Double> groupAvgAge = getGroupAvgAge(list);
        groupAvgAge.forEach((g,age)-> System.out.println("第"+g+"组的平均年龄是"+age));
        List<Student> oldest = get3Oldest(list);
        System.out.println("三个最老的是:");
        for (Student student : oldest) {
            System.out.println(student);
        }
        List<Student> youngest = get3Youngest(list);
        System.out.println("三个最小的是:");
        for (Student student : youngest) {
            System.out.println(student);
        }
        Student oldest1 = getOldest(list);
        System.out.println("最老的是"+oldest1);
        Student youngest1 = getYoungest(list);
        System.out.println("最小的是"+youngest1);
        Period between = Period.between(oldest1.getBirthday(), youngest1.getBirthday());

        System.out.println("两个人差"+between.getYears()+"年"+between.getMonths()+"月"+between.getDays()+"天");

        Map<String, Integer> xingNumber = getXingNumber(list);
        xingNumber.forEach((k,v)->
            System.out.println("姓"+k+"的人有"+v+"个"));
        Map<Integer, Integer> ageNumber = getAgeNumber(list);
        ageNumber.forEach((k,v)->
                System.out.println("年龄是"+k+"的人有"+v+"个"));


    }

    //男生和女生各自有多少人？    可以
    public static int getNan(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null && stu.getGender().equals("男")) {
                count++;
            }
        }
        return count;
    }

    public static int getNv(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null && stu.getGender().equals("女")) {
                count++;
            }
        }
        return count;
    }


    //00后有多少人？    可以
    public static int get00(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null) {
                LocalDate birthday = stu.getBirthday();
                int year = birthday.getYear();
                if (year >= 2000) {
                    count++;
                }
            }
        }
        return count;
    }


    //名字长度是2的学生有多少人？ 可以
    public static int getName2(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null) {
                int length = stu.getName().length();
                if (length == 2) {
                    count++;
                }
            }
        }
        return count;
    }

    //名字长度是3的学生有多少人？ 可以
    public static int getName3(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null) {
                int length = stu.getName().length();
                if (length == 3) {
                    count++;
                }
            }
        }
        return count;
        //   long count = list.stream().filter(s -> s.getName().length() == 3).count();
        //        System.out.println(count);
    }

    //姓张的同学有多少人？  可以
    public static int getZhang(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null) {
                if (stu.getName().startsWith("张")) {
                    count++;
                }
            }
        }
        return count;
    }

    //手机号中至少包含2个6的有哪些？  可以
    public static int getPhone66(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null) {
                String phone = stu.getPhone();
                int index = 0;
                char[] chars = phone.toCharArray();
                for (int i1 = 0; i1 < chars.length; i1++) {
                    if (chars[i1] == '6') {
                        index++;
                    }
                }
                if (index >= 2) {
                    count++;
                }
            }
        }
        return count;
    }

    //手机号中至少包含1个6和1个8的有哪些？  可以
    public static int getPhone68(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null) {
                String phone = stu.getPhone();
                if (phone.contains("6") && phone.contains("8")) {
                    count++;
                }
            }
        }
        return count;
    }

    //有2个的微信号的人个数  可以
    public static int get2WX(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null) {
                int size = stu.getWxs().size();
                if (size == 2) {
                    count++;
                }
            }
        }
        return count;
    }

    //有1个的微信号的人个数   可以
    public static int get1WX(ArrayList<Student> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu != null) {
                int size = stu.getWxs().size();
                if (size == 1) {
                    count++;
                }
            }
        }
        return count;
    }

    //各个小组的平均年龄？
    //返回值类型 Map<Integer,Double>
    //参数  所有的学生对象 ArrayList<Student>
    public static Map<Integer, Double> getGroupAvgAge(ArrayList<Student> list) {
        //将每个组的的人放在一起
        Map<Integer, ArrayList<Student>> map = new TreeMap<>();
        for (Student student : list) {
            //获取学生的组
            int group = student.getGroup();
            //判断map中是否包含指定的组
            boolean flag = map.containsKey(group);
            if (!flag) {//不存在
                ArrayList<Student> groupList = new ArrayList<>();
                groupList.add(student);
                map.put(group,groupList);
            }else{
                ArrayList<Student> groupList = map.get(group);
                groupList.add(student);
                map.put(group,groupList);
            }
        }
        //统计他们的平均年龄
        Map<Integer, Double> result= new TreeMap<>();
        map.forEach((g,grouplist)->{
            double sum=0;
            for (Student student : grouplist) {
                //累加本组内的所有年龄
                sum+=student.getAge();
            }
            double avgAge=sum/grouplist.size();
            result.put(g,avgAge);
        });
        return result;
    }

    //获取最大年纪的三个人
    public static List<Student> get3Oldest(ArrayList<Student> list){
        TreeSet<Student> treeSet = getStudentsByBirthday(list);
        return treeSet.stream().limit(3).collect(Collectors.toList());
    }

    private static TreeSet<Student> getStudentsByBirthday(ArrayList<Student> list) {
        //将学生放入treeSet里
        TreeSet<Student> treeSet = new TreeSet<>((s1, s2) -> {
            LocalDate birthday1 = s1.getBirthday();
            LocalDate birthday2 = s2.getBirthday();
            /*int year1 = birthday1.getYear();
            int year2 = birthday2.getYear();
            int day1 = birthday1.getDayOfYear();
            int day2 = birthday2.getDayOfYear();
            int i = year2 - year1;
            int i1 = i == 0 ? day2 - day1 : i;*/
            int i1 = birthday1.compareTo(birthday2);
            return i1 == 0 ? s1.getName().compareTo(s2.getName()) : i1;
        });
        for (Student student : list) {
            treeSet.add(student);
        }
        return treeSet;
    }

    //获取最小年纪的三个人
    public static List<Student> get3Youngest(ArrayList<Student> list){
        //将学生放入treeSet里
        TreeSet<Student> treeSet = getStudentsByBirthday(list);
        return treeSet.stream().skip(list.size()-3).collect(Collectors.toList());
    }

    //获取最小年纪的人
    public static Student getYoungest(ArrayList<Student> list){
        //将学生放入treeSet里
        TreeSet<Student> treeSet = getStudentsByBirthday(list);
        return treeSet.last();
    }
    //获取最大年纪的人
    public static Student getOldest(ArrayList<Student> list){
        //将学生放入treeSet里
        TreeSet<Student> treeSet = getStudentsByBirthday(list);
        return treeSet.first();
    }


    //不同姓的人的个数？
    //返回值类型 Map<String,Integer>
    //参数  所有的学生对象 ArrayList<Student>
    public static Map<String,Integer> getXingNumber(ArrayList<Student> list) {
        //将每个组的的人放在一起
        Map<String, ArrayList<Student>> map = new TreeMap<>();
        for (Student student : list) {
            //获取学生的姓
            String xing = student.getName().substring(0, 1);
            //判断map中是否包含指定的组
            boolean flag = map.containsKey(xing);
            if (!flag) {//不存在
                ArrayList<Student> groupList = new ArrayList<>();
                groupList.add(student);
                map.put(xing,groupList);
            }else{
                ArrayList<Student> groupList = map.get(xing);
                groupList.add(student);
                map.put(xing,groupList);
            }
        }
        //统计他们的平均年龄
        Map<String, Integer> result= new TreeMap<>();
        map.forEach((g,grouplist)->{
            int sum=0;
            for (Student student : grouplist) {
                //累加本组内的所有年龄
                sum++;
            }
            result.put(g,sum);
        });
        return result;
    }
    //不同年龄段的个数？
    //返回值类型 Map<String,Integer>
    //参数  所有的学生对象 ArrayList<Student>
    public static Map<Integer,Integer> getAgeNumber(ArrayList<Student> list) {
        //将每个组的的人放在一起
        Map<Integer, ArrayList<Student>> map = new TreeMap<>();
        for (Student student : list) {
            //获取学生的姓
            int age = student.getAge();
            //判断map中是否包含指定的组
            boolean flag = map.containsKey(age);
            if (!flag) {//不存在
                ArrayList<Student> groupList = new ArrayList<>();
                groupList.add(student);
                map.put(age,groupList);
            }else{
                ArrayList<Student> groupList = map.get(age);
                groupList.add(student);
                map.put(age,groupList);
            }
        }
        //统计他们的平均年龄
        Map<Integer, Integer> result= new TreeMap<>();
        map.forEach((g,grouplist)->{
            int sum=0;
            for (Student student : grouplist) {
                //数一数这个年龄段有几个人
                sum++;
            }
            result.put(g,sum);
        });
        return result;
    }
}
